package com.kede.app.service.impl;

import com.alipay.api.domain.Category;
import com.kede.app.domain.LeescoreCategory;
import com.kede.app.ienum.CateStatusEnum;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.LeescoreCategoryRepository;
import com.kede.app.service.LeescoreCategoryService;
import com.kede.app.service.dto.LeescoreCategoryDto;
import com.kede.app.service.dto.LeescoreCategoryQueryCriteria;
import com.kede.app.service.mapstruct.LeescoreCategoryMapper;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-05-27
**/
@Service
@RequiredArgsConstructor
public class LeescoreCategoryServiceImpl implements LeescoreCategoryService {

    private final LeescoreCategoryRepository leescoreCategoryRepository;
    private final LeescoreCategoryMapper leescoreCategoryMapper;

    @Override
    public Map<String,Object> queryAll(LeescoreCategoryQueryCriteria criteria, Pageable pageable){
        Page<LeescoreCategory> page = leescoreCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(leescoreCategoryMapper::toDto));
    }

    @Override
    public List<LeescoreCategoryDto> queryAll(LeescoreCategoryQueryCriteria criteria){
        return leescoreCategoryMapper.toDto(leescoreCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LeescoreCategoryDto findById(Integer id) {
        LeescoreCategory leescoreCategory = leescoreCategoryRepository.findById(id).orElseGet(LeescoreCategory::new);
        ValidationUtil.isNull(leescoreCategory.getId(),"LeescoreCategory","id",id);
        return leescoreCategoryMapper.toDto(leescoreCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeescoreCategoryDto create(LeescoreCategory resources) {
        return leescoreCategoryMapper.toDto(leescoreCategoryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LeescoreCategory resources) {
        LeescoreCategory leescoreCategory = leescoreCategoryRepository.findById(resources.getId()).orElseGet(LeescoreCategory::new);
        ValidationUtil.isNull( leescoreCategory.getId(),"LeescoreCategory","id",resources.getId());
        leescoreCategory.copy(resources);
        leescoreCategoryRepository.save(leescoreCategory);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            leescoreCategoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LeescoreCategoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LeescoreCategoryDto leescoreCategory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("菜单名", leescoreCategory.getName());
            map.put("顶级父类ID", leescoreCategory.getTopid());
            map.put("上级菜单", leescoreCategory.getCategoryId());
            map.put("完整路径", leescoreCategory.getPath());
            map.put("权重排序", leescoreCategory.getWeigh());
            map.put("状态", leescoreCategory.getStatus());
            map.put("创建时间", leescoreCategory.getCreatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<LeescoreCategory> getNormalCate(Integer pid) {
        LeescoreCategory category = new LeescoreCategory();
        category.setCategoryId(pid);
        category.setStatus(CateStatusEnum.NORMAL.getCode());
        Sort weigh = Sort.by(Sort.Direction.ASC, "weigh");
        return leescoreCategoryRepository.findAll(Example.of(category), weigh);
    }
}